<?php
session_start();
if (isset($_SESSION["account"])) {
?>
<?php require_once('common/header.php'); ?>
<?php require_once('common/sidebar.php'); ?> 
        <div class="main-content app-content mt-0">
                <div class="side-app">
<!--占位-->
<div class="card"> </div>
<!--占位-->
<!--这里开始查询代理的账户信息-->
<?php
    include_once("../untils/conn.php");
    mysqli_query($con, "set names utf8");
    // 获取当前正在登录的账号信息的代理
    $proxy_acc = mysqli_real_escape_string($con, $_SESSION["account"]);
    $sql = "SELECT * FROM proxy WHERE proxy_acc = ?";
    $stmt = mysqli_prepare($con, $sql);
    mysqli_stmt_bind_param($stmt, "s", $proxy_acc);
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    
    if (mysqli_num_rows($result) > 0) {
        // 获取相应数据
        $row = mysqli_fetch_assoc($result);
        // 余额
        $proxy_balance = $row["proxy_balance"];
        // 支付宝信息
        $proxy_alipay = $row["proxy_alipay"];
        $proxy_alipay_acc = $row["proxy_alipay_acc"];
        // 银行信息
        $proxy_yihname = $row["proxy_yihname"];
        $proxy_yhnum = $row["proxy_yhnum"];
        $proxy_bank_name = $row["proxy_bank_name"];
        $proxy_bank_zh = $row["proxy_bank_zh"];
      
    }
?>

<!--这里结束查询-->
                    <!-- CONTAINER -->
                    <div class="main-container container-fluid">
                
                        <!-- PAGE-HEADER -->
                        
                    <div class="row row-sm">
                        <div class="col-lg-12">
                            <div class="card">
                                <div class="card-header">
                                    <button type="button" class="btn btn btn-info me-3  mt-2" data-bs-toggle="modal" data-bs-target="#input-modal" data-bs-whatever="@mdo">申请提现</button>
                                    <h3 class="card-title">提现记录</h3>
                                </div>
                                <div class="card-body">
                                    <div class="table-responsive">
                                        <table class="table table-bordered text-nowrap border-bottom" id="responsive-datatable">
                                            <thead>
                                                <tr>
                                                    <th class="wd-10p border-bottom-0">提现账号</th>
                                                    <th class="wd-10p border-bottom-0">姓名</th>
                                                    <th class="wd-10p border-bottom-0">账号</th>
                                                    <th class="wd-10p border-bottom-0">银行名称</th>
                                                    <th class="wd-10p border-bottom-0">银行支行</th>
                                                    <th class="wd-10p border-bottom-0">提现金额</th>
                                                    <th class="wd-10p border-bottom-0">打款金额</th>
                                                    <th class="wd-10p border-bottom-0">提现方式</th>
                                                    <th class="wd-10p border-bottom-0">提现状态</th>
                                                    <th class="wd-10p border-bottom-0">申请时间</th>
                                                    <th class="wd-10p border-bottom-0">处理时间</th>
                                                    <th class="wd-10p border-bottom-0">备注</th>
                                                    
                                                </tr>
                                            </thead>
                                            <tbody id="txdata">
                                                
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                    <!-- CONTAINER CLOSED -->

                </div>
            </div>
            <!-- Inout modal -->
    <div class="modal fade" id="input-modal">
        <div class="modal-dialog" role="document">
            <div class="modal-content modal-content-demo">
                <div class="modal-header">
                    <h6 class="modal-title">申请提现</h6>
                    <button class="btn-close" data-bs-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form id="withdrawal-form" method="post" action="controller/Dowith.php">
                      <div class="mb-3">
                          <label for="recipient-name" class="col-form-label">提现金额:<p style="color:blue">可提现余额:<?php echo $proxy_balance ?></p></label>
                          <input type="text" class="form-control" id="recipient-name" name="tx_price" value="0.00" onblur="checkWithdrawalAmount()">
                        </div>
                      <div class="mb-3">
                        <label for="recipient-name" class="col-form-label">提现方式:<p style="color:red">企业请使用对公账号，否则审核不通过</p></label>
                        <select class="form-control" id="tx_type" name="tx_type" size="1" onchange="changeWithdrawalType()">
                                <!--<option value="0">微信</option>-->
                                <option value="1">支付宝</option>
                                <option value="2">银行账号</option>
                              </select>
                      </div>
                      <div id="alipay-info" style="display:none;">
                        <div class="mb-3">
                          <label for="alipay-name" class="col-form-label">支付宝姓名:</label>
                          <input type="text" class="form-control" id="alipay-name" name="alipay_name" value="<?php echo $proxy_alipay ?>">
                        </div>
                        <div class="mb-3">
                          <label for="alipay-account" class="col-form-label">支付宝账号:</label>
                          <input type="text" class="form-control" id="alipay-account" name="alipay_account" value="<?php echo $proxy_alipay_acc ?>">
                        </div>
                      </div>
                      <div id="bank-info" style="display:none;">
                        <div class="mb-3">
                          <label for="bank-name" class="col-form-label">姓名:</label>
                          <input type="text" class="form-control" id="tx_name" name="tx_name" value="<?php echo $proxy_yihname ?>">
                        </div>
                        <div class="mb-3">
                          <label for="bank-account" class="col-form-label">银行账号:</label>
                          <input type="text" class="form-control" id="tx_acc" name="tx_acc" value="<?php echo $proxy_yhnum ?>">
                        </div>
                        <div class="mb-3">
                          <label for="bank-account" class="col-form-label">银行名称:</label>
                          <input type="text" class="form-control" id="bank_name" name="bank_name" value="<?php echo $proxy_bank_name ?>">
                        </div>
                        <div class="mb-3">
                          <label for="bank-branch" class="col-form-label">开户行支行名称:</label>
                          <input type="text" class="form-control" id="bank-branch" name="bank_branch" value="<?php echo $proxy_bank_zh ?>">
                        </div>
                      </div>
                       <!--时间戳-->
                          <input type="hidden" name="cr_time" value="<?php echo time(); ?>">
                          <!--提现人账号-->
                          <input type="hidden" name="proxy_acc" value="<?php echo $proxy_acc ?>">
                    </form>
                  </div>
                <div class="modal-footer">
                    <button class="btn ripple btn-success" type="submit" form="withdrawal-form">提交申请</button>
                    <button class="btn ripple btn-danger" data-bs-dismiss="modal" type="button">取消</button>
                </div>
            </div>
        </div>
    </div>
    
      <?php
        session_start();
        include_once("../untils/conn.php");
        mysqli_query($con, "set names utf8");
        
        // 获取当前正在登录的账号信息的代理
        $proxy_acc = $_SESSION["account"];
        
        // 查询proxy表中的数据
        $query = "SELECT proxy_rename_sta FROM proxy WHERE proxy_acc = '$proxy_acc'";
        $result = mysqli_query($con, $query);
        $row = mysqli_fetch_assoc($result);
        
        if ($row) {
            $proxy_rename_sta = $row['proxy_rename_sta'];
        ?>
        
        <div class="modal fade" id="modaldemo5" data-bs-backdrop="static" data-bs-keyboard="false">
            <div class="modal-dialog modal-dialog-centered text-center" role="document">
                <div class="modal-content tx-size-sm">
                    <div class="modal-body text-center p-4 pb-5">
                        <i class="icon icon-close fs-70 text-danger lh-1 my-5 d-inline-block"></i>
                        <h4 class="text-danger">警告: 您的账号未实名！</h4>
                        <p class="mg-b-20 mg-x-20">平台要求所有代理商必须实名！否则无法使用平台功能！</p>
                        <a href="profile.php" class="btn btn-primary pd-x-25">去实名</a>
                    </div>
                </div>
            </div>
        </div>
        
        
            <!--/div-->
    <script src="assets/js/jquery.min.js"></script>
    <script>
      $(document).ready(function() {
  // 发起 Ajax 请求
  $.ajax({
    url: "controller/Withdrawal.php",
    type: "GET",
    dataType: "json",
    success: function(data) {
      // 渲染数据到表格中
      var html = "";
      $.each(data, function(index, item) {
        html += "<tr>";
        html += "<td>" + item.proxy_acc + "</td>";
        html += "<td>" + item.tx_name + "</td>";
        html += "<td>" + item.tx_acc + "</td>";
        html += "<td>" + item.tx_bank_name + "</td>";
        html += "<td>" + item.tx_bank_zh + "</td>";
        html += "<td>" + (item.tx_price ? "￥" + item.tx_price : "无") + "</td>";
        html += "<td>" + (item.dk_price ? "￥" + item.dk_price : "无") + "</td>";
        html += "<td>" + (item.tx_type == 0 ? "微信" : item.tx_type == 1 ? "支付宝" : item.tx_type == 2 ? "银行卡" : "无") + "</td>";
        html += "<td>" + (item.state == 0 ? "待审核" : item.state == 1 ? "已打款" : item.state == 2 ? "审核失败" : "无") + "</td>";
        html += "<td>" + item.cr_time + "</td>";
        html += "<td>" + item.cl_time + "</td>";
        html += "<td>" + (item.notes ? item.notes : "无") + "</td>";
        html += "</tr>";
      });
      $("#txdata").html(html);
    },
    error: function(xhr, status, error) {
      console.log("Error: " + error);
    }
  });
});


    function checkWithdrawalAmount() {
      var inputElement = document.getElementById("recipient-name");
      var inputValue = parseFloat(inputElement.value);
      var balance = parseFloat("<?php echo $proxy_balance ?>");
    
      if (inputValue > balance) {
        inputElement.value = "0.00";
      }
    }

    function changeWithdrawalType() {
      var txType = document.getElementById("tx_type").value;
      var alipayInfo = document.getElementById("alipay-info");
      var bankInfo = document.getElementById("bank-info");

      if (txType == "1") {
        alipayInfo.style.display = "block";
        bankInfo.style.display = "none";
      } else if (txType == "2") {
        alipayInfo.style.display = "none";
        bankInfo.style.display = "block";
      } else {
        alipayInfo.style.display = "none";
        bankInfo.style.display = "none";
      }
    }
    </script>
    <script>
    $(document).ready(function() {
        <?php
        if ($proxy_rename_sta == 0) {
            // 未实名认证
            echo '$("#modaldemo5").modal("show");';
            echo '$("#modaldemo5").modal({backdrop: "static", keyboard: false});';
        } elseif ($proxy_rename_sta == 1) {
            // 已实名认证
            echo '$("#modaldemo5").modal("hide");';
        }
        ?>
    });
</script>

<?php
} else {
    // 未找到相关数据
    // echo '数据不存在';
}
?>
    <!-- End Datepicker modal -->
 <?php require_once('common/footer.php'); ?>   
  <?php
} else {
  echo "<script>alert('登录过过期，没有权限访问该页面');location.href='login.html';</script>";
}
?>
